package com.hjh.netty.elect.util;

import com.hjh.netty.elect.demo4.StringDataConverUtils;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 数值转换工具
 * @author pyz
 *
 */
public class NumberDataConvertUtils {
	
	/**
	 * 十进制转16进制
	 * @param decimalString
	 * @return
	 */
	public static String decimalString2hexString(String decimalString) {
		//将十进制转为十六进制
		String result = Integer.toHexString(Integer.parseInt(decimalString));
		//长度不够2的倍数前面补0
		if((result.length() % 2) != 0) {
			result = "0" + result;
		}
        return result.toUpperCase();
	}
	
	/**
	 * 16 进制转10进制
	 * @param hexString
	 * @return int型数值
	 */
	public static int hexString2decimalInt(String hexString) {
		//16进制转10进制
        BigInteger sint = new BigInteger(hexString, 16);
        return sint.intValue();
	}
	
	/**
     * 将16进制转换为二进制
     *
     * @param hexString
     * @return 8位长度字符
     */
    public static String hexString2binaryString(String hexString) {
        //16进制转10进制
        BigInteger sint = new BigInteger(hexString, 16);
        //10进制转2进制
        String result = sint.toString(2);
        //不够要在前面补0
        return StringDataConverUtils.dataAutoLength(result, 8);
    }
    
    /**
     *  2进制转16进制
     * @param binaryString
     * @return
     */
    public static String binaryString2hexString(String binaryString) {
    	//将二进制转为十进制
    	int num = Integer.parseInt(binaryString, 2);
    	//将十进制转为十六进制
    	String result = Integer.toHexString(num);
    	if(result.length() < 2) {
    		result = "0" + result;
    	}
    	return result.toUpperCase();
    }
    
    /**
     *  十进制转二进制
     * @param decimal
     * @return 8位长度字符
     */
    public static String decimalString2binary(int decimal) {
    	//不够要在前面补0
    	return StringDataConverUtils.dataAutoLength(Integer.toBinaryString(decimal), 8);
    }
    
    /**
     *  十进制转二进制
     * @param decimal 十进制数值
     * @param length 要保留的位数，不够在前面补0
     * @return length位长度字符
     */
    public static String decimalString2binary(int decimal, int length) {
    	//不够要在前面补0
    	return StringDataConverUtils.dataAutoLength(Integer.toBinaryString(decimal), length);
    }
    
    /**
     * 二进制转十进制
     * @param binaryString
     * @return
     */
    public static String binaryStrings2decimalString(String binaryString) {
    	String result = String.valueOf(Integer.parseInt(binaryString, 2));
    	if(result.length() < 2) {
    		result = "0" + result;
    	}
    	return result;
    }
    
    /**
     *  16进制转为10进制浮点型，保留两位小数，后面的直接去掉
     *  将 4字节的16进制字符串，转换为32位带符号的十进制浮点型
     * @param hexFloatString
     * @return
     */
    public static float hexFloatString2decimalFloatString(String hexFloatString) {
    	float ff = Float.intBitsToFloat(new BigInteger(hexFloatString.trim(), 16).intValue());
    	BigDecimal bigDecimal = new BigDecimal(ff);
    	//保留两位小数，后面的直接去掉
    	return bigDecimal.setScale(2, BigDecimal.ROUND_DOWN).floatValue();
    }
    
    /**
     * 10进制浮点型转为16进制
     * 将带符号的32位浮点数装换为16进制
     * @param decimalFloat
     * @return
     */
    public static String decimalFloat2StringhexFloatString(Float decimalFloat) {
    	return Integer.toHexString(Float.floatToIntBits(decimalFloat));
    }
    
    public static void main(String[] args) {
    	//01000001 41  -6.36
		System.out.println(hexFloatString2decimalFloatString("0000005E"));
    	System.out.println(hexString2decimalInt("0000005E"));
		//System.out.println(binaryString2hexString("01000001"));
    	//System.out.println(hexString2decimalString("41"));
    	/*String s="c4fc9300";
    	Float value = hexFloatString2decimalFloatString(s);
        System.out.println("16进制浮点数转10进制="+value);
        Float f=-2020.59375f;
        System.out.println("10进制浮点数转16进制="+decimalFloat2StringhexFloatString(f));*/
		/*String ss = "B388AF8C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005E005F0060006400650066006A006B006C00700071007200760077007800C100C200C300C700C800C900CD00CE00CF00D300D400D500D900DA00DB";
		System.out.println(ss.length());*/
	}
}

